//
//  AsyncSocketManager+RAC.m
//  高仿暴走斗图
//
//  Created by key on 16/4/1.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "AsyncSocketManager+RAC.h"
#import "AsyncSocket+RACSignal.h"

@implementation AsyncSocketManager (RAC)

- (RACSignal *)rac_didConnectToHostForSocket:(AsyncSocket *)socket {
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        [[[socket rac_didConnectToHost] deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(id x) {
            
            [subscriber sendNext:x];
            [subscriber sendCompleted];
            
        }];
        return nil;
    }];
}


- (RACSignal *)rac_onSocketDidDisconnectForSocket:(AsyncSocket *)socket {
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        [[[socket rac_onSocketDidDisconnect] deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(id x) {
            
            [subscriber sendNext:x];
            [subscriber sendCompleted];
        }];
        return nil;
    }];
}


- (RACSignal *)rac_willDisconnectForSocket:(AsyncSocket *)socket {
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        [[[socket rac_willDisconnect] deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(id x) {
            
            [subscriber sendNext:x];
            [subscriber sendCompleted];
        }];
        return nil;
    }];
}


- (RACSignal *)rac_didReadDataForSocket:(AsyncSocket *)socket {
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        [[[socket rac_didReadData] deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(id x) {
            
            [subscriber sendNext:x];
            [subscriber sendCompleted];
        }];
        return nil;
    }];
}


- (RACSignal *)rac_didWriteDataForSocket:(AsyncSocket *)socket {
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        [[[socket rac_didWriteDataWithTag] deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(id x) {
            
            [subscriber sendNext:x];
            [subscriber sendCompleted];
        }];
        return nil;
    }];
}

@end
